package com.socool.sinofaith.ui;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.adapter.CurrentTaskAdapter;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.PublicTaskMessage;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.SinofaithUtil;
import com.socool.sinofaith.view.LoadingDialog;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class PublicTaskFragment extends Fragment {

	private PullToRefreshListView mPullRefreshListView;
	private ListView listView;
	private CurrentTaskAdapter mCurrentTaskAdapter;

	private int currentIndex = 1;

	private LoadingDialog mLoadingDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_publictask, null);
		SocoolApplication.addActivity(this.getActivity());
		mPullRefreshListView = (PullToRefreshListView) view
				.findViewById(R.id.listview);
		
		return view;
	}
	
	void init(){
		mLoadingDialog = new LoadingDialog(this.getActivity());
		mCurrentTaskAdapter = new CurrentTaskAdapter(this.getActivity());
		
		listView = mPullRefreshListView.getRefreshableView();
		listView.setAdapter(mCurrentTaskAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(PublicTaskFragment.this
						.getActivity(), TaskDetailActivity.class);
				intent.putExtra("task",
						mCurrentTaskAdapter.getMdatas().get(arg2 - 1));
				PublicTaskFragment.this.getActivity().startActivity(intent);

			}
		});

		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						String label = DateUtils.formatDateTime(
								PublicTaskFragment.this.getActivity(),
								System.currentTimeMillis(),
								DateUtils.FORMAT_SHOW_TIME
										| DateUtils.FORMAT_SHOW_DATE
										| DateUtils.FORMAT_ABBREV_ALL);

						refreshView.getLoadingLayoutProxy()
								.setLastUpdatedLabel(label);

						// 清空数据重新请求
						mCurrentTaskAdapter.getMdatas().clear();
						currentIndex = 1;
						getData();
					}
				});

		mPullRefreshListView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
					@Override
					public void onLastItemVisible() {
						// lastLoadingView.setVisibility(View.VISIBLE);
						currentIndex++;
						getData();
					}
				});
		
		// 清空数据重新请求
		mCurrentTaskAdapter.getMdatas().clear();
		currentIndex = 1;
		getData();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		MainActivity.imageview.setVisibility(View.VISIBLE);
		MainActivity.imageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// 清空数据重新请求
				mCurrentTaskAdapter.getMdatas().clear();
				currentIndex = 1;
				getData();
			}
		});
		super.onActivityCreated(savedInstanceState);
	}
	
	@Override
	public void onResume() {
		init();
		super.onResume();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	void getData() {
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.getPublicTask_url);
		params.put("did", SinofaithUtil.did);
		params.put("mobile", getSPString());
		params.put("pageNum", currentIndex + "");
		params.put("pageSize", ConstansJsonUrl.pageSize + "");
		fh.get(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack() {

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mLoadingDialog.show();
			}

			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				mLoadingDialog.dismiss();
				super.onSuccess(t);
				try {
					PublicTaskMessage publicTaskMessage = HttpJsonAdapter
							.getInstance().get(t.toString(),
									PublicTaskMessage.class);
					mCurrentTaskAdapter.addDatas(publicTaskMessage
							.getPublicTaskList());
					mCurrentTaskAdapter.notifyDataSetChanged();

					mPullRefreshListView.onRefreshComplete();
				} catch (Exception e) {
					mLoadingDialog.dismiss();
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				mLoadingDialog.dismiss();
				super.onFailure(t, errorNo, strMsg);
				Toast.makeText(PublicTaskFragment.this.getActivity(), strMsg,
						Toast.LENGTH_LONG).show();
			}

		});
	}
	private String getSPString(){
		SharedPreferences sp=getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}
}
